package it.polimi.traveldream.entity;

import it.polimi.traveldream.dto.DefaultPackageDTO;
import it.polimi.traveldream.dto.DetailedDefaultPackageDTO;
import it.polimi.traveldream.dto.ScheduleDTO;

import java.io.Serializable;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotEmpty;

/**
 * The persistent class for the defaultpackage database table.
 * 
 */
@Entity
@Table(name = "defaultpackage")
@DiscriminatorValue("default")
@NamedQuery(name = DefaultPackage.FIND_ALL, query = "SELECT d FROM DefaultPackage d WHERE d.valid = 1")
public class DefaultPackage extends Package implements Serializable {
	private static final long serialVersionUID = 1L;

	public static final String FIND_ALL = "DefaultPackage.findAll";

	private String description;

	private String image;

	@NotEmpty
	private String name;

	@NotNull
	private Boolean valid;

	public DefaultPackage() {
	}

	public DefaultPackage(DefaultPackageDTO packDTO) {
		this.setId(packDTO.getId());
		this.setDescription(packDTO.getDescription());
		this.setCity(packDTO.getCity());
		this.setDuration(packDTO.getDuration());
		this.setImage(packDTO.getImage());
		this.setName(packDTO.getName());
		this.setPrice(packDTO.getPrice());
		this.setValid(packDTO.getValid());
	}

	public DefaultPackage(DetailedDefaultPackageDTO packDTO) {
		this((DefaultPackageDTO) packDTO);
		this.setAccomodation(new Accomodation(packDTO.getAccomodation()));
		this.setGoing(new Transport(packDTO.getGoing()));
		this.setBack(new Transport(packDTO.getBack()));
		float sum = (packDTO.getAccomodation().getPrice() * packDTO.getDuration())
				+ packDTO.getGoing().getPrice() + packDTO.getBack().getPrice();
		for (ScheduleDTO sched : packDTO.getSchedule().values()) {
			this.addSchedule(new Schedule(sched));
			sum += sched.getActivity().getPrice();
		}
		this.setPrice(sum);
	}

	public DefaultPackageDTO convertToDTO() {
		DefaultPackageDTO packDTO = new DefaultPackageDTO();
		packDTO.setId(this.getId());
		packDTO.setDescription(this.getDescription());
		packDTO.setCity(this.getCity());
		packDTO.setDuration(this.getDuration());
		packDTO.setImage(this.getImage());
		packDTO.setName(this.getName());
		packDTO.setPrice(this.getPrice());
		packDTO.setValid(this.getValid());
		return packDTO;
	}

	public DetailedDefaultPackageDTO convertToDetailedDTO() {
		DetailedDefaultPackageDTO packDTO = new DetailedDefaultPackageDTO();
		packDTO.setId(this.getId());
		packDTO.setDescription(this.getDescription());
		packDTO.setCity(this.getCity());
		packDTO.setDuration(this.getDuration());
		packDTO.setImage(this.getImage());
		packDTO.setName(this.getName());
		packDTO.setPrice(this.getPrice());
		packDTO.setValid(this.getValid());
		packDTO.setAccomodation(this.getAccomodation().convertToDTO());
		packDTO.setGoing(this.getGoing().convertToDTO());
		packDTO.setBack(this.getBack().convertToDTO());
		for (Schedule sched : this.getSchedule()) {
			packDTO.addSchedule(sched.convertToDTO());
		}
		return packDTO;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getImage() {
		return this.image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Boolean getValid() {
		return this.valid;
	}

	public void setValid(Boolean valid) {
		this.valid = valid;
	}

}